package com.heraclito.heraclitoapp;

import model.LoginDialog;

import com.heraclito.heraclitoapp.R;
import com.samsung.sprc.fileselector.FileOperation;
import com.samsung.sprc.fileselector.FileSelector;
import com.samsung.sprc.fileselector.OnHandleFileListener;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Color;
//import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SubMenu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TabHost;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;

import webservices.Logoff;

public class TabActivity extends ActivityGroup {
	public static TabHost tabHost;
	public static int prova = 1;
	public static int logado = 0;
	public static String usuario;
	public static String senha;
	public static Button btn_login = null;
	public static TextView logadoComo;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_abas);
		
		btn_login = (Button) findViewById(R.id.btn_login);
		logadoComo = (TextView) findViewById(R.id.logadoComo);
		
		// Resources res = getResources();
		tabHost = (TabHost) findViewById(R.id.tabHost);
		tabHost.setBackgroundColor(Color.WHITE);
		tabHost.setup(this.getLocalActivityManager());
		TabHost.TabSpec spec;
		Intent intent;

		intent = new Intent().setClass(this, HeraclitoActivity.class);
		spec = tabHost
				.newTabSpec(getResources().getString(R.string.prova) + prova)
				.setIndicator(getResources().getString(R.string.prova) + prova)
				.setContent(intent);
		tabHost.addTab(spec);
		tabHost.getTabWidget().getChildTabViewAt(0)
				.setTag(getResources().getString(R.string.prova) + prova);

		LoginDialog loginDialog = new LoginDialog(this);
		loginDialog.show();
		// Intent login = new Intent().setClass(this, LoginActivity.class);
		// startActivity(login);

		tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			
			public void onTabChanged(String tabId) {
				if (tabId.equals(getResources().getString(R.string.manual)) || tabId.equals(getResources().getString(R.string.exemplos))) {
					btn_login.setVisibility(View.INVISIBLE);
					logadoComo.setVisibility(View.INVISIBLE);
				} else {
					btn_login.setVisibility(View.VISIBLE);
					logadoComo.setVisibility(View.VISIBLE);
				}
			}
		});
		
		Button btn_aba = (Button) findViewById(R.id.btn_aba);

		btn_aba.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				TabHost.TabSpec spec2;
				Intent intent2;
				final int numTab;

				intent2 = new Intent().setClass(TabActivity.this,
						HeraclitoActivity.class);
				prova++;
				spec2 = tabHost
						.newTabSpec(
								getResources().getString(R.string.prova)
										+ prova)
						.setIndicator(
								getResources().getString(R.string.prova)
										+ prova).setContent(intent2);
				numTab = tabHost.getTabWidget().getTabCount();
				tabHost.addTab(spec2);
				tabHost.getTabWidget().getChildTabViewAt(numTab).setTag(getResources().getString(R.string.prova)+ prova);

								// tabHost.getTabWidget().removeView();
								// tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt());
								// Toast.makeText(getBaseContext(), v.getId(),
								// Toast.LENGTH_SHORT).show();

			}
		});
		

		btn_login.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				if (logado == 0) {
					LoginDialog loginDialog = new LoginDialog(TabActivity.this);
					loginDialog.show();
				} else {
					new Logoff(TabActivity.this).execute(usuario);
				}
			
			}
		});
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, getResources().getString(R.string.salvar_prova));
		menu.add(0, 2, 0, getResources().getString(R.string.carregar_prova));
		menu.add(0, 3, 0, getResources().getString(R.string.fechar_aba));
		// SubMenu subMenu = menu.addSubMenu(0, 1, 2, "Texto");
		// menu.getItem(index)
		// subMenu.add
		// subMenu.add(0, 2, 1, "texto");
		// subMenu.add(0, 3, 2, "texto");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		final String[] mFileFilter = {".her"};

		OnHandleFileListener mLoadFileListener = new OnHandleFileListener() {
			
			public void handleFile(final String filePath) {
				Toast.makeText(TabActivity.this, getResources().getString(R.string.carregando) + filePath,
						Toast.LENGTH_SHORT).show();
				String tabTag = tabHost.getCurrentTabTag(); 
				Activity activityAtual = getLocalActivityManager().getActivity(tabTag);
				if (activityAtual instanceof HeraclitoActivity) {
					((HeraclitoActivity) activityAtual).Abrir(filePath);
				}
			}
		};
			
		OnHandleFileListener mSaveFileListener = new OnHandleFileListener() {

				public void handleFile(final String filePath) {
					Toast.makeText(TabActivity.this, getResources().getString(R.string.salvando) + filePath,
							Toast.LENGTH_SHORT).show();
					String tabTag = tabHost.getCurrentTabTag(); 
					Activity activityAtual = getLocalActivityManager().getActivity(tabTag);
					if (activityAtual instanceof HeraclitoActivity) {
						((HeraclitoActivity) activityAtual).Salvar(filePath);
					}
					
				}
		};


		switch (item.getItemId()) {
		case 1:
			new FileSelector(this, FileOperation.SAVE, mSaveFileListener,
					mFileFilter).show();
			break;
		case 2:
			new FileSelector(this, FileOperation.LOAD, mLoadFileListener,
					mFileFilter).show();
			break;
		case 3:
			if (tabHost.getCurrentTab() == 0) {
				Toast.makeText(this, getResources().getString(R.string.primeira_aba), Toast.LENGTH_LONG).show();
			} else {
				tabHost.setCurrentTab(tabHost.getCurrentTab() - 1);
				tabHost.getTabWidget().getChildTabViewAt(tabHost.getCurrentTab() + 1).setVisibility(View.GONE);
				//tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(tabHost.getCurrentTab() + 1));
				}
			}
		return true;
	}

}